/**
 * 
 */
package fr.upvm.lem3.vm3d.ui.swing.volume3d;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;
import javax.swing.plaf.DimensionUIResource;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

import fr.upvm.lem3.vm3d.domain.Channel;

/**
 * 
 *******************************************************************
 *Information.java
 *04.03.2012
 *@author Guy Nougouapi
 *******************************************************************
 *
 *******************************************************************
 */
public class Information extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6375431833660124832L;

	private static final String DATA_ARRAYS = "Data Arrays";
	private static final String DIMS = "Dims";
	private static final String X_DIM = "X-Dim: ";
	private static final String Y_DIM = "Y-Dim: ";
	private static final String Z_DIM = "Z-Dim: ";
	private static final String STEPS = "Steps";
	private static final String X_STEP = "X-Step: ";
	private static final String Y_STEP = "Y-Step: ";
	private static final String Z_STEP = "Z-Step:";

	JLabel lblXDim, lblYDim, lblZDim;
	JLabel lblXStep, lblYStep, lblZStep;
	private JPanel paneldata; 
	private JPanel panelDim, panelStep;
	private InformationController controller;
	private MyTableModel myTableModel;
	private JTable table;
	
	public Information (){
		super();
		this.setController(new InformationController(this));
//		myTableModel.setData(new Vector<Object>(getController().getCustomObjects()));
		
		initComponents();
	}
	
	/**
	 * 
	 */
	private void initComponents(){
		setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
		add(getPanelDim());
		add(Box.createRigidArea(new Dimension(0,2)));
		add(getPanelStep());
		add(Box.createRigidArea(new Dimension(0,2)));
		add(getPaneldata());
		add(Box.createRigidArea(new Dimension(0,5)));
	}
	/**
	 * @return the paneldata
	 */
	public JPanel getPaneldata() {
		if(paneldata == null){
			paneldata = new JPanel(new BorderLayout());
			paneldata.setBorder(null);
			paneldata.setBorder(BorderFactory.createTitledBorder(
					BorderFactory.createMatteBorder(1, 1, 1, 1, Color.LIGHT_GRAY), 
					DATA_ARRAYS,  TitledBorder.LEFT, TitledBorder.TOP));
			JScrollPane srlpane = new JScrollPane(getTable());
			srlpane.setBackground(Color.white);
			srlpane.setBorder(BorderFactory.createEmptyBorder(5,0,0,5));
			paneldata.add(srlpane,BorderLayout.CENTER);
			paneldata.setMaximumSize(new Dimension(paneldata.getPreferredSize().width, paneldata.getPreferredSize().height));
			paneldata.setOpaque(true);
		}
		return paneldata;
	}
	/**
	 * @param paneldata the paneldata to set
	 */
	public void setPaneldata(JPanel paneldata) {
		this.paneldata = paneldata;
	}
	/**
	 * @return the panelDim
	 */
	public JPanel getPanelDim() {
		if(panelDim == null){
			panelDim = new JPanel();
			panelDim.setLayout(new GridLayout(3, 2 ));
			panelDim.setBorder(BorderFactory.createTitledBorder(
			 		BorderFactory.createMatteBorder(1, 1, 1, 1, Color.LIGHT_GRAY), 
			 		DIMS,  TitledBorder.LEFT, TitledBorder.TOP));
			/**
			 * create add the labels to this panel
			 */			 
			panelDim.add(new JLabel(X_DIM ,JLabel.CENTER));
			panelDim.add(getXDim());
			panelDim.add(new JLabel(Y_DIM,JLabel.CENTER));
			panelDim.add(getYDim());
			panelDim.add(new JLabel(Z_DIM,JLabel.CENTER));
			panelDim.add(getZDim());
			panelDim.setPreferredSize(new Dimension(panelDim.getPreferredSize().width, panelDim.getPreferredSize().height));
		}
		return panelDim;
	}
	/**
	 * @param panelDim the panelDim to set
	 */
	public void setPanelDim(JPanel panelDim) {
		this.panelDim = panelDim;
	}
	
	
	public JLabel getZDim() {
		if(lblZDim == null){
			lblZDim = new JLabel(Z_DIM, JLabel.LEFT);
		}
		return lblZDim;
	}

	public JLabel getYDim() {
		if(lblYDim == null){
			lblYDim = new JLabel(Y_DIM, JLabel.LEFT);
		}
		return lblYDim;
	}

	public JLabel getXDim() {
		if(lblXDim == null){
			lblXDim = new JLabel(X_DIM, JLabel.LEFT);
		}
		return lblXDim;
	}

	/**
	 * @return the panelStep
	 */
	public JPanel getPanelStep() {
		if(panelStep == null){
			panelStep = new JPanel();
			panelStep.setLayout(new GridLayout(3, 2,0,0));
			panelStep.setBorder(BorderFactory.createTitledBorder(
			 		BorderFactory.createMatteBorder(1, 1, 1, 1, Color.LIGHT_GRAY), 
			 		STEPS,  TitledBorder.LEFT, TitledBorder.TOP));
			/**
			 * create add the labels to this panel
			 */
			panelStep.add(new JLabel(X_STEP, JLabel.CENTER));
			panelStep.add(getXStep());
			panelStep.add(new JLabel(Y_STEP, JLabel.CENTER));
			panelStep.add(getYStep());
			panelStep.add(new JLabel(Z_STEP, JLabel.CENTER));
			panelStep.add(getZStep());
			panelStep.setPreferredSize(new DimensionUIResource(panelStep.getPreferredSize().width, panelStep.getPreferredSize().height));
		}
		return panelStep;
	}
	/**
	 * @param panelStep the panelStep to set
	 */
	public void setPanelStep(JPanel panelStep) {
		this.panelStep = panelStep;
	}

	public JLabel getZStep() {
		if(lblZStep == null){
			lblZStep = new JLabel(Z_STEP, JLabel.LEFT);
		}
		return lblZStep;
	}

	public JLabel getYStep() {
		if(lblYStep == null){
			lblYStep = new JLabel(Y_STEP, JLabel.LEFT);
		}
		return lblYStep;
	}

	public JLabel getXStep() {
		if(lblXStep == null){
			lblXStep = new JLabel(X_STEP, JLabel.LEFT);
		}
		return lblXStep;
	}

/**
	 * @return the controller
	 */
	public InformationController getController() {
		return controller;
	}
	/**
	 * @param controller the controller to set
	 */
	public void setController(InformationController controller) {
		this.controller = controller;
	}



/**
	 * @return the table
	 */
	public JTable getTable() {
		if(table == null){
			table = new JTable(getMyTableModel());
			/**** custom the cell of this table with our custom cellrenderer****/
//		    for(int i = 0; i< getMyTableModel().getColumnCount(); i++){
//		    	table.getColumnModel().getColumn(i).setCellRenderer(new CustomCellrenderer());
//		    }
		   /********/
			table.getTableHeader().setPreferredSize(new Dimension(-1, 20));
			table.getTableHeader().setBorder(BorderFactory.createMatteBorder(1, 1, 0, 1, Color.black));
			table.getTableHeader().setAlignmentX(JTable.CENTER_ALIGNMENT);
			table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			table.setShowGrid(false);
			table.setFillsViewportHeight(false);
			table.setBorder(BorderFactory.createMatteBorder(0, 1, 1, 1, Color.black));
			table.setAlignmentX(JTable.CENTER_ALIGNMENT);
			table.setMaximumSize(new Dimension(table.getPreferredSize().width, ((int)table.getPreferredSize().width/2)));
		}
		return table;
	}
	/**
	 * @param table the table to set
	 */
	public void setTable(JTable table) {
		this.table = table;
	}



/**
	 * @return the myTableModel
	 */
	public MyTableModel getMyTableModel() {
		if(myTableModel == null){
			myTableModel = new MyTableModel(new Vector<Channel>());		
		}
		return myTableModel;
	}

	/**
	 * @param myTableModel the myTableModel to set
	 */
	public void setMyTableModel(MyTableModel myTableModel) {
		this.myTableModel = myTableModel;
	}



/**
 * Implementation of a custom tablemodel
 * 
 * @author Nougouapi
 * 
 */
public class MyTableModel extends AbstractTableModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String NAME = "Name";
	private static final String DATA_TYPE = "Data Type";
	private static final String DATA_RANGES = "Data Range";

	private Vector<String> columnNames;
	private Vector<Channel> data;
	
	public MyTableModel(Vector<Channel> vector) {
		super();
		this.setData(vector);
		columnNames = new Vector<String>();
		columnNames.add(NAME);
		columnNames.add(DATA_TYPE);
		columnNames.add(DATA_RANGES);
	
	}
	
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return columnNames.size();
	}

	public int getRowCount() {
		// TODO Auto-generated method stub
		return getData().size();
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getValueAt(int, int) call when the
	 * table model get fill
	 */
	public Object getValueAt(int row, int col) {
		Object res = null;
		Channel ch = getData().get(row);
		switch (col) {
		case 0:
			res = (Object)("  "+ch.getName());
			break;
		case 1:
			res = (Object)("  "+ch.getDataType());
			break;
		case 2:
			NumberFormat format = NumberFormat.getInstance(Locale.ENGLISH);
			format.setMaximumFractionDigits(4);
			
			String str = " ["+ String.valueOf(format.format(ch.getLowerLimit())) +" , "+String.valueOf(format.format(ch.getUpperLimit()))+"]";
			res = (Object)str;
			break;
		default:
			break;
		}
		return res;

	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
	 */
	public boolean isCellEditable(int row, int col) {
		// all cells are not editable
		return false;
	}

	/**
	 * 
	 * @param col
	 * @return
	 */
	public String getColumnName(int col) {
		return this.columnNames.get(col);
	}
    
	public void setData(Vector<Channel> data) {
		this.data = data;
		this.fireTableDataChanged();
	}
	
	public void setData(Channel[] data) {
		Vector<Channel> vec = new Vector<Channel>();
		for(Channel ch : data)
			vec.add(ch);
		this.data = vec;
		this.fireTableDataChanged();
	}
	public Vector<Channel> getData() {
		return data;
	}

}// innere Klasse : Ende

/**
 * this class implement the renderer of the table
 *******************************************************************
 *Information.java
 *04.03.2012
 *@author Guy Nougouapi
 *******************************************************************
 *
 *******************************************************************
 */
class CustomCellrenderer extends DefaultTableCellRenderer {

		private static final long serialVersionUID = 7456873445671290743L;

		public Component getTableCellRendererComponent (JTable table, 
				   Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
				         Component cell = super.getTableCellRendererComponent(
				                            table, obj, isSelected, hasFocus, row, column);
				         Color bgcol = cell.getBackground();
				         if (isSelected) {
							 cell.setForeground(Color.black);
							 cell.setBackground(new Color(0, 0, 255,25));
				         }
				         else 
				        	 if (row % 2 == 0) {
				             cell.setBackground(bgcol);
				             cell.setForeground(Color.black);
				           }
				           else {
				             cell.setBackground(bgcol);
				             cell.setForeground(Color.black);
				           }
				         return cell;
				       }
		  }
}
